#include "b_config.h"
#include "b_os.h"
#include "ciu32l051_std.h"
#include "hal/inc/b_hal_uart.h"
bHalUartCallback_t usart1_callback;
/**
 * @brief  USART1 ÖÐ¶Ï·þÎñº¯Êý
 * @retval ÎÞ
 */
void USART1_IRQHandler(void)
{
    // 检查错误中断
    if ((std_usart_get_cr1_interrupt_enable(USART1, USART_CR1_INTERRUPT_RXNE) ||
         std_usart_get_cr3_interrupt_enable(USART1, USART_CR3_INTERRUPT_ERR)) &&
        std_usart_get_flag(USART1, USART_FLAG_ORE))
    {
        // 清除错误标志
        std_usart_clear_flag(USART1, USART_CLEAR_ORE);
    }
    // 检查接收中断
    if (std_usart_get_cr1_interrupt_enable(USART1, USART_CR1_INTERRUPT_RXNE) &&
        std_usart_get_flag(USART1, USART_FLAG_RXNE))
    {
        // 读取数据
        uint8_t data = std_usart_rx_read_data(USART1);
        usart1_callback(&data, 1, NULL);
    }
 
}

int bMcuUartInit(bHalUartNumber_t uart, bHalUartConfig_t *config)
{
    if (uart == B_HAL_UART_1)
    {
        std_gpio_init_t gpio_config = {0};
        std_rcc_gpio_clk_enable(RCC_PERIPH_CLK_GPIOA);

        gpio_config.pin         = GPIO_PIN_1 | GPIO_PIN_2;
        gpio_config.mode        = GPIO_MODE_ALTERNATE;
        gpio_config.output_type = GPIO_OUTPUT_PUSHPULL;
        gpio_config.pull        = GPIO_PULLUP;
        gpio_config.alternate   = GPIO_AF1_USART1;
        std_gpio_init(GPIOA, &gpio_config);

        /* USARTÊ±ÖÓÊ¹ÄÜ */
        std_rcc_apb2_clk_enable(RCC_PERIPH_CLK_USART1);

        std_usart_init_t usart_init_t = {0};

        usart_init_t.baudrate      = 115200;
        usart_init_t.wordlength    = USART_WORDLENGTH_8BITS;
        usart_init_t.stopbits      = USART_STOPBITS_1;
        usart_init_t.parity        = USART_PARITY_NONE;
        usart_init_t.hardware_flow = USART_FLOWCONTROL_NONE;
        usart_init_t.direction     = USART_DIRECTION_SEND_RECEIVE;
        if (STD_OK != std_usart_init(USART1, &usart_init_t))
        {
            while (1)
                ;
        }
        std_usart_enable(USART1);
        NVIC_SetPriority(USART1_IRQn, NVIC_PRIO_1);
        NVIC_EnableIRQ(USART1_IRQn);
        /* 使能接收中断 */

        std_usart_cr1_interrupt_enable(USART1, USART_CR1_INTERRUPT_RXNE);
        return 0;
    }

    return -1;
}

int bMcuUartSend(bHalUartNumber_t uart, const uint8_t *pbuf, uint16_t len)
{
        if (uart == B_HAL_UART_1)
        {
            for (uint16_t i = 0; i < len; i++)
            {
                uint32_t timeout = 0;
                while (!(std_usart_get_flag(USART1, USART_FLAG_TXE)))
                {
                    timeout++;
                    if (timeout > 0xFFFFF)
                    {
                        break;
                    }
                }
                std_usart_tx_write_data(USART1, pbuf[i]);
            }
        }
    
    return 0;
}

int bMcuReceive(bHalUartNumber_t uart, uint8_t *pbuf, uint16_t len)
{
    return -1;
}

int bMcuUartRegisterCallback(bHalUartNumber_t uart, bHalUartCallback_t callback)
{
    if (uart == B_HAL_UART_1)
    {
        usart1_callback = callback;
    }
    return 0;
}

